package com.songVersion.controller.actions;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.songVersion.model.Cover;
import com.songVersion.model.Search;
import com.songVersion.model.SearchedAuthor;
import com.songVersion.model.Song;
import com.songVersion.model.ThumbnailCache;
import com.songVersion.model.Version;
import com.songVersion.nest.NestSearch;
import com.songVersion.services.CoverManager;
import com.songVersion.services.SearchManager;
import com.songVersion.services.SearchedAuthorManager;
import com.songVersion.services.VersionManager;
import com.songVersion.services.impl.CoverManagerImpl;
import com.songVersion.services.impl.SearchManagerImpl;
import com.songVersion.services.impl.SearchedAuthorManagerImpl;
import com.songVersion.services.impl.VersionManagerImpl;

public abstract class GeneralAction implements Action
{
	private HttpServletRequest req;
	private String songName = "";
	private String songAuthor = "";
	private int page;


	
	public void init(HttpServletRequest req)
	{
		setReq(req);
		setPage(1);
		setSongName(getParameter("songName"));
		setSongAuthor(getParameter("songAuthor"));
	}
	
	protected void setAttribute(String name, Object value)
	{
		getReq().setAttribute(name, value);
	}

	protected void getMaxPlayedVersions()
	{
		VersionManager vm = new VersionManagerImpl();
		List<Version> l = vm.getMaxSearched(10);
		setAttribute("topVersions", l);
	}
	
	protected void getMaxPlayedCovers()
	{
		CoverManager cm = new CoverManagerImpl();
		cm.setLoadParodies(false);
		List<Cover> l = cm.getMaxSearched(10);
		
		setAttribute("topCovers", l);
		
	}
	
	protected void getMaxPlayedParodies()
	{
		CoverManager cm = new CoverManagerImpl();
		cm.setLoadParodies(true);
		List<Cover> l = cm.getMaxSearched(10);
		
		setAttribute("topParodies", l);
		
	}
	
	protected  void getMaxSearched()
	{
		SearchManager sm = new SearchManagerImpl();
		List<Search> topSearch = sm.getMaxSearched(10);
		setAttribute("topSearch", topSearch);
	}
	
	protected void getMaxAuthorsSearched()
	{
		SearchedAuthorManager sam = new SearchedAuthorManagerImpl();
		List<SearchedAuthor> topAuthors = sam.getMaxSearched(10);
		setAttribute("topAuthors", topAuthors);
	}

	protected int getIntParameter(String param)
	{
		String sValue = getReq().getParameter(param);
		if (sValue != null && !sValue.equals(""))
			;
		int res = Integer.parseInt(sValue);
		return res;
	}

	protected String getParameter(String param)
	{
		String res;
		if (getReq().getAttribute(param) != null)
			res = (String) getReq().getAttribute(param);
		else
		{
			res = getReq().getParameter(param);
			getReq().setAttribute(param, res);
		}
		return res;
	}
	
	protected int getVersionsInNest()
	{
		NestSearch ns = new NestSearch(getSongName(), getSongAuthor(), page);
		List<Song> l = ns.search();
		setAttribute("versions", l);
		return l.size();
	}

	public HttpServletRequest getReq()
	{
		return req;
	}

	public void setReq(HttpServletRequest req)
	{
		this.req = req;
	}

	public String getSongName()
	{
		return songName;
	}

	public void setSongName(String songName)
	{
		this.songName = songName;
	}

	public String getSongAuthor()
	{
		return songAuthor;
	}

	public void setSongAuthor(String songAuthor)
	{
		this.songAuthor = songAuthor;
	}

	public int getPage()
	{
		return page;
	}

	public void setPage(int page)
	{
		this.page = page;
	}
}
